package org.gl.customview;

import java.util.Calendar;

import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.text.InputType;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.TimePicker;

import com.chanceit.ssss.activity.R;

/**
 * used to form edit on android
 * 
 * @author Administrator
 * 
 */
public class EditView extends LinearLayout {
	public EditView(Context context) {
		super(context);
		init(context);
	}
	int postion;
	public static final int Cennter = 0;
	public static final int Top = 1;
	public static final int Bottom = 2;
	View top;
	View bottom;
	View typeIcon;
	Context context;
	private Drawable inputTypeIcon;
	private Handler handler;
	private void init(Context context) {
		this.context = context;
		LayoutInflater mLayoutInflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		view = mLayoutInflater.inflate(R.layout.editview, null);
		leftTextView = (TextView) view.findViewById(R.id.textView);
		editView = (EditText) view.findViewById(R.id.editText);
		top = view.findViewById(R.id.top);
		bottom = view.findViewById(R.id.botm);
		if (a != null) {
			try {
				leftTextView.setText(a.getText(R.styleable.EditView_leftText));
				if (a.getInt(R.styleable.EditView_leftTextColor, 0x00000000) != 0x00000000) {
					leftTextView.setTextColor(a.getInt(
							R.styleable.EditView_leftTextColor, 0x00000000));
				}
				if (a.getFloat(R.styleable.EditView_editTextSize, 0) != 0x00000000) {
					leftTextView.setTextSize(a.getFloat(
							R.styleable.EditView_editTextSize, 0));
				}

				editView.setHint(a.getText(R.styleable.EditView_editHint));
				if (a.getInt(R.styleable.EditView_editTextColor, 0x00000000) != 0x00000000) {
					editView.setTextColor(a.getInt(
							R.styleable.EditView_editTextColor, 0x00000000));
				}
				if (a.getFloat(R.styleable.EditView_editTextSize, 0) != 0x00000000) {
					editView.setTextSize(a.getFloat(
							R.styleable.EditView_editTextSize, 0));
				}
				postion = a.getInt(R.styleable.EditView_Position, 0x00000000);
				inputTypeIcon =a.getDrawable(R.styleable.EditView_inputTypeIcon);
				switch (postion) {
				default:
				case Cennter:
					top.setVisibility(View.GONE);
					bottom.setVisibility(View.VISIBLE);
					break;
				case Top:
					top.setVisibility(View.GONE);
					bottom.setVisibility(View.VISIBLE);
					break;
				case Bottom:
					top.setVisibility(View.GONE);
					bottom.setVisibility(View.GONE);
					editView.setImeOptions(EditorInfo.IME_ACTION_DONE);
					break;
				}
				int inputType = a.getInt(R.styleable.EditView_InputType,
						0x00000000);
				switch (inputType) {
				case 9:// text
					editView.setInputType(InputType.TYPE_TEXT_VARIATION_PHONETIC);//pinyin
					break;
				case 10:// num
					editView.setInputType(InputType.TYPE_CLASS_NUMBER);
					break;
				case 11:// date
					handler = new Handler();
					handler.postDelayed(new Runnable() {//FIXME find the typeIconView will case XML inflate err in the view  constructor.why?
						@Override
						public void run() {
							typeIcon = view.findViewById(R.id.inputTypeIcon);
							typeIcon.setVisibility(View.VISIBLE);
							if (inputTypeIcon!=null) {
								typeIcon.setBackground(inputTypeIcon);
							}
							
						}
					},2000);
					editView.setInputType(InputType.TYPE_CLASS_DATETIME);
					editView.setFocusable(false);
					
					editView.setOnClickListener(new BtnOnClickListener(1));
					break;
				case 12:// password
					editView.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);
					break;
				case 13:// phone
					editView.setInputType(InputType.TYPE_CLASS_PHONE);
					break;

				default:
					break;
				}
			} finally {
				a.recycle();
			}
		}
		this.addView(view, ViewGroup.LayoutParams.WRAP_CONTENT,
				ViewGroup.LayoutParams.WRAP_CONTENT);
	}
//    <enum name="numb" value="10"></enum>
//    <enum name="text" value="9"></enum>
//    <enum name="date" value="11" ></enum>
//    <enum name="password" value="12" ></enum>
//    <enum name="phone" value="13" ></enum>
	View view;
	TextView leftTextView;
	EditText editView;
	private TypedArray a;

	public EditView(Context context, AttributeSet attrs) {
		super(context, attrs);
		TypedArray a = context.getTheme().obtainStyledAttributes(attrs,
				R.styleable.EditView, 0, 0);
		this.a = a;
		init(context);
	}

	public EditView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		TypedArray a = context.getTheme().obtainStyledAttributes(attrs,
				R.styleable.EditView, 0, 0);
		this.a = a;
		init(context);
	}

	public void setLeftTextView(String text) {
		leftTextView.setText(text);
	}

	public View getView() {
		return view;
	}

	public void setView(View view) {
		this.view = view;
	}

	public TextView getLeftTextView() {
		return leftTextView;
	}

	public void setLeftTextView(TextView leftTextView) {
		this.leftTextView = leftTextView;
	}

	public EditText getEditView() {
		return editView;
	}
	public void setEditText(String text) {
		if ("null"==text) {
			text ="";
		}
		editView.setText(text);
	}

	public void setErr(String error) {
		getEditView().setError(error);
	}

	private final int DATE_DIALOG = 1;

	private final int TIME_DIALOG = 2;

	protected Dialog onCreateDialog(int id) {
		// 用来获取日期和时间的
		Calendar calendar = Calendar.getInstance();

		Dialog dialog = null;
		switch (id) {
		case DATE_DIALOG:
			DatePickerDialog.OnDateSetListener dateListener = new DatePickerDialog.OnDateSetListener() {
				@Override
				public void onDateSet(DatePicker datePicker, int year,
						int month, int dayOfMonth) {
					EditText editText = (EditText) findViewById(R.id.editText);
					// Calendar月份是从0开始,所以month要加1
					String monthString = (month + 1)<10?"0"+(month + 1):(""+(month + 1));
					String dayOfMonthString = dayOfMonth<10?"0"+(dayOfMonth):(""+(dayOfMonth));
					editText.setText(""+year+"-"+ monthString +"-"+ dayOfMonthString);
				}
			};
			dialog = new DatePickerDialog(context, dateListener,
					calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
					calendar.get(Calendar.DAY_OF_MONTH));
			break;
		case TIME_DIALOG:
			TimePickerDialog.OnTimeSetListener timeListener = new TimePickerDialog.OnTimeSetListener() {

				@Override
				public void onTimeSet(TimePicker timerPicker, int hourOfDay,
						int minute) {
					EditText editText = (EditText) findViewById(R.id.editText);
					editText.setText(""+hourOfDay+":"+ minute );
				}
			};
			dialog = new TimePickerDialog(context, timeListener,
					calendar.get(Calendar.HOUR_OF_DAY),
					calendar.get(Calendar.MINUTE), false); // 是否为二十四制
			break;
		default:
			break;
		}
		return dialog;
	}

	/*
	 * 成员内部类,此处为提高可重用性，也可以换成匿名内部类
	 */
	private class BtnOnClickListener implements View.OnClickListener {

		private int dialogId = 0; // 默认为0则不显示对话框

		public BtnOnClickListener(int dialogId) {
			this.dialogId = dialogId;
		}

		@Override
		public void onClick(View view) {
			onCreateDialog(dialogId).show();
			;
		}

	}
}
